

using System;

using Tao.Sdl;

using System.IO;

using System.Runtime.InteropServices;

using System.Threading;



 

namespace myapp

{

 

    static class Program

    {

 

       

        enum Action { Draw, Drawc,playsound };

 

        static void Main()

        {

 

            Sdl.SDL_Init(Sdl.SDL_INIT_EVERYTHING);

 

            Sdl.SDL_WM_SetCaption("Hello Wrold", "");

 

            IntPtr surfacePtr = Sdl.SDL_SetVideoMode(800, 600, 0, Sdl.SDL_HWSURFACE | Sdl.SDL_DOUBLEBUF | Sdl.SDL_ANYFORMAT);

 

            CheckForKeyPress(surfacePtr);

        }


        static void CheckForKeyPress(IntPtr surfacePtr)

        {



            

           

            bool quitFlag = false;

            bool sflag = false;

     

            int result;

 

            Sdl.SDL_Event evt;


            while (quitFlag == false)


            {

             

                result = Sdl.SDL_PollEvent(out evt);

 

                if (evt.type == Sdl.SDL_QUIT)

                {

 

                    quitFlag = true;

 

                }

           else if (evt.type == Sdl.SDL_MOUSEBUTTONDOWN)

                {
				 Sdl.SDL_WM_SetCaption("Y-" + evt.motion.y.ToString() + " X-" + evt.motion.x.ToString(), "");
					
					font(surfacePtr,"Y-" + (short)evt.motion.y,150,150);
					font(surfacePtr,"X-" + (short)evt.motion.x,150,245);
				
				}

                else if (evt.type == Sdl.SDL_KEYDOWN)

                {

 

                    if ((evt.key.keysym.sym == (int)Sdl.SDLK_ESCAPE) ||

 

                        (evt.key.keysym.sym == (int)Sdl.SDLK_q))

                    {

 

                        quitFlag = true;

 

                    }

 

                    else if ((evt.key.keysym.sym == (int)Sdl.SDLK_SPACE))

                    {

                       

                        if (sflag == true)

                        {

                            sflag = false;

 

                        }

                        else

                        {

                            sflag = true;

                        }

                                     

                    }

                    else if ((evt.key.keysym.sym == (int)Sdl.SDLK_UP))

                    {

                        CheckStatus(Action.Draw, surfacePtr);

                    }

 

                    else if ((evt.key.keysym.sym == (int)Sdl.SDLK_p))

                    {

                        CheckStatus(Action.playsound, surfacePtr);

                    }

                    

                }

 

                if (sflag == true) { 

                CheckStatus(Action.Drawc, surfacePtr);

            }

            }

 

        }
 

        static void draw(IntPtr surfacePtr)

        {
 

            Sdl.SDL_Rect srcrect = new Sdl.SDL_Rect(0, 0, 325, 250);

 


 

            Sdl.SDL_Rect dstrect = new Sdl.SDL_Rect(100, 150, 0, 0);

 

            Sdl.SDL_Rect dstrect2 = new Sdl.SDL_Rect(375, 150, 0, 0);

 

            string fileName = "../../Data/NeHe.Lesson11.Tim.bmp";

            string fileName2 = "../../Data/NeHe.Lesson11.Tim.bmp";

 

            IntPtr newsurface = Sdl.SDL_LoadBMP(fileName);

 

            IntPtr newsurface2 = Sdl.SDL_LoadBMP(fileName2);

 

            // Sdl.SDL_SetClipRect(surfacePtr, ref cropsize);

 

            Sdl.SDL_BlitSurface(newsurface, ref srcrect, surfacePtr, ref dstrect);

            Sdl.SDL_BlitSurface(newsurface2, ref srcrect, surfacePtr, ref dstrect2);

 

            Sdl.SDL_Flip(surfacePtr);

              

        }

 

        static void drawcir(IntPtr surfacePtr)

        {

               Random rand = new Random();

               SdlGfx.filledCircleColor(
			                         surfacePtr, 
			                        (short)rand.Next(0, 300),
			                         (short)rand.Next(0, 300), 
			                         (short)rand.Next(0, 150), 
			                         (short)rand.Next(0, 100000)
			                         );

                Sdl.SDL_Flip(surfacePtr);
			 

        }


            static void CheckStatus(Action type, IntPtr surfacePtr)

            {

 

                if (type == Action.Draw)

                {

                    draw(surfacePtr);

                }

 

                else if (type == Action.Drawc)

                {

                    drawcir(surfacePtr);

                }

 

                else if (type == Action.playsound)

                {

                    playmusic();

                }

 

            }

 

 

        static void playmusic()

        {

            int result = SdlMixer.Mix_OpenAudio(

             SdlMixer.MIX_DEFAULT_FREQUENCY,

              (short)SdlMixer.MIX_DEFAULT_FORMAT,

               2,

                1024);

          

           IntPtr chunkPtr = SdlMixer.Mix_LoadMUS("../../Data/NeHe.Lesson21.Die.wav");

          SdlMixer.Mix_PlayMusic(chunkPtr, 1);

        

        

        }
		
		static void font(IntPtr surfacePtr,string text,int x,int y)
		{
		
			SdlTtf.TTF_Init();
			Sdl.SDL_Color fg = new Tao.Sdl.Sdl.SDL_Color(255,255,255,0);
				
			IntPtr ftpt  = SdlTtf.TTF_OpenFont("../../Font/36daythk.ttf",75);
			
			IntPtr fontrenderpt = SdlTtf.TTF_RenderText_Blended(ftpt,text,fg);
			SdlTtf.TTF_CloseFont(ftpt);
			
			Sdl.SDL_Rect srcrect = new Sdl.SDL_Rect(0, 0, 300,800);
 
			Sdl.SDL_Rect dstrect = new Sdl.SDL_Rect((short)x, (short)y, 0, 0);	
		
			
			Sdl.SDL_BlitSurface(fontrenderpt,ref srcrect , surfacePtr, ref dstrect);
			
			
		
			Sdl.SDL_Flip(surfacePtr);
			
			Sdl.SDL_FreeSurface(fontrenderpt);
			
			
		}

     }

 

  }